//
//  MyChangePassVC.m
//  shortvideoplay
//
//  Created by tikeios_002 on 2024/7/1.
//

#import "MyChangePassVC.h"

@interface MyChangePassVC ()<UITextFieldDelegate>
@property (strong, nonatomic) YBTextFild * loginTF_name;
@property (strong, nonatomic) YBTextFild * loginTF_pass;
@property (strong, nonatomic) UIButton * loginBtn_pass;
@property (strong, nonatomic) UIButton * loginBtn_login;
@property (assign, nonatomic) int passNum;
@property (assign, nonatomic) int cutTimerNum;
@property (strong, nonatomic) NSTimer* cutTimer;
@end

@implementation MyChangePassVC

-(void)showTextViewNavViewTo1{
    NSMutableArray * showTextViewNavViewNameArrTo1 = [NSMutableArray new];
    UIView * showTextViewNavViewViewTo1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
    showTextViewNavViewViewTo1.clipsToBounds = YES;
       UILabel * oldshowSSSSTip = [[UILabel alloc]init];
    oldshowSSSSTip.clipsToBounds = YES;
    oldshowSSSSTip.text = @"kass显示区";
    oldshowSSSSTip.textColor = ColorHex(@"#f0f0f0");
    oldshowSSSSTip.textAlignment = NSTextAlignmentCenter;
    oldshowSSSSTip.font = [UIFont systemFontOfSize:28];
    oldshowSSSSTip.hidden = YES;
    [self.view addSubview:oldshowSSSSTip];
    [self.view addSubview:showTextViewNavViewViewTo1];
    UILabel * showTextViewNavViewLabelTo1 = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    NSInteger showTextViewNavViewNumberTo1 = arc4random_uniform(20);
    NSLog(@"：%ld", (long)showTextViewNavViewNumberTo1);
    showTextViewNavViewLabelTo1.text = [NSString stringWithFormat:@"%ld",(long)showTextViewNavViewNumberTo1];
    [showTextViewNavViewLabelTo1 addSubview:showTextViewNavViewLabelTo1];
    
    UIImageView * showTextViewNavViewLabelTo2 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    [showTextViewNavViewViewTo1 addSubview:showTextViewNavViewLabelTo2];
    
}
- (void)showOldAddNavViewToMainOldView1{
    UIView * oldBgSS = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
    oldBgSS.clipsToBounds = YES;
       UILabel * oldshowSSSSTip = [[UILabel alloc]init];
    oldshowSSSSTip.clipsToBounds = YES;
    oldshowSSSSTip.text = @"kass显示区";
    oldshowSSSSTip.textColor = ColorHex(@"#f0f0f0");
    oldshowSSSSTip.textAlignment = NSTextAlignmentCenter;
    oldshowSSSSTip.font = [UIFont systemFontOfSize:28];
    oldshowSSSSTip.hidden = YES;
    [self.view addSubview:oldshowSSSSTip];
    [self.view addSubview:oldBgSS];
    
    UIButton * sendBtn = [[UIButton alloc]initWithFrame:CGRectMake(KScreen_W-75, kStatusBarHeight+10, 60, 46)];
    sendBtn.layer.cornerRadius = 3;
    sendBtn.backgroundColor = [UIColor whiteColor];
    [sendBtn setTitle:@"发送" forState:UIControlStateNormal];
    [sendBtn setTitleColor:ColorHex(@"#333333") forState:UIControlStateNormal];
    sendBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [sendBtn addTarget:self action:@selector(sendTap) forControlEvents:UIControlEventTouchUpInside];
    [oldBgSS addSubview:sendBtn];
    
    UIView * sendBg = [[UIView alloc]initWithFrame:CGRectMake(15, kStatusBarHeight+66, KScreen_W-30, (KScreen_H-KSafeAreaBottomHeight-25-(kStatusBarHeight+66))/2)];
    sendBg.backgroundColor = [UIColor whiteColor];
    sendBg.layer.cornerRadius = 3;
    [oldBgSS addSubview:sendBg];
    
    UILabel * sendTip = [[UILabel alloc]initWithFrame:CGRectMake(0, (sendBg.frame.size.height-40)/2, sendBg.frame.size.width, 40)];
    sendTip.text = @"发送区";
    sendTip.textColor = ColorHex(@"#f0f0f0");
    sendTip.textAlignment = NSTextAlignmentCenter;
    sendTip.font = [UIFont systemFontOfSize:28];
    [sendBg addSubview:sendTip];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = viewback_black;
//    self.navigationItem.title = @"修改手机号";
    
    // Do any additional setup after loading the view.
    UILabel * tipLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 20, KScreen_W-40, 20)];
    tipLabel.textAlignment = NSTextAlignmentLeft;
    tipLabel.textColor = [UIColor whiteColor];
    tipLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
    tipLabel.numberOfLines = 0;
       UILabel * oldshowSSSSTip = [[UILabel alloc]init];
    oldshowSSSSTip.clipsToBounds = YES;
    oldshowSSSSTip.text = @"kass显示区";
    oldshowSSSSTip.textColor = ColorHex(@"#f0f0f0");
    oldshowSSSSTip.textAlignment = NSTextAlignmentCenter;
    oldshowSSSSTip.font = [UIFont systemFontOfSize:28];
    oldshowSSSSTip.hidden = YES;
    [self.view addSubview:oldshowSSSSTip];
    [self.view addSubview:tipLabel];
    
    UILabel * subtipLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 42, KScreen_W-40, 18)];
    subtipLabel.alpha = 0.77;
    subtipLabel.textAlignment = NSTextAlignmentLeft;
    subtipLabel.textColor = [UIColor whiteColor];
    subtipLabel.font = [UIFont systemFontOfSize:14];
    subtipLabel.text = @"新手机通过验证后可以换绑，需要重新登录";
    subtipLabel.numberOfLines = 0;
       UILabel * oldshowSSSSTip1 = [[UILabel alloc]init];
    oldshowSSSSTip1.clipsToBounds = YES;
    oldshowSSSSTip1.text = @"kass显示区";
    oldshowSSSSTip1.textColor = ColorHex(@"#f0f0f0");
    oldshowSSSSTip1.textAlignment = NSTextAlignmentCenter;
    oldshowSSSSTip1.font = [UIFont systemFontOfSize:28];
    oldshowSSSSTip1.hidden = YES;
    [self.view addSubview:oldshowSSSSTip1];
    [self.view addSubview:subtipLabel];
    
    UIView * left_name_view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 60, 50)];
    UILabel * codelabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
    codelabel.textColor = [UIColor whiteColor];
    codelabel.alpha = 0.72;
    codelabel.font = Font(15);
    codelabel.text = @"+86";
    codelabel.textAlignment = NSTextAlignmentCenter;
    [left_name_view addSubview:codelabel];
    
    UIView * left_name_line = [[UIView alloc]initWithFrame:CGRectMake(50, 17, 0.7, 16)];
    left_name_line.backgroundColor = [UIColor whiteColor];
    left_name_line.alpha = 0.72;
    [left_name_view addSubview:left_name_line];
    
    self.loginTF_name = [[YBTextFild alloc]initWithFrame:CGRectMake(20, 85, KScreen_W-40, 50)];
    self.loginTF_name.formaterType = YBAFormatterTextFieldFormatterTypePhone;
    self.loginTF_name.font = Font(15);
    self.loginTF_name.textColor = [UIColor whiteColor];
    self.loginTF_name.delegate = self;
    self.loginTF_name.keyboardType = UIKeyboardTypeNumberPad;
    self.loginTF_name.layer.cornerRadius = 8;
    self.loginTF_name.leftView = left_name_view;
    self.loginTF_name.leftViewMode = UITextFieldViewModeAlways;
    self.loginTF_name.tintColor = MainColor;
    self.loginTF_name.backgroundColor = subviewback_black;
    [self.view addSubview:self.loginTF_name];

    self.loginTF_pass = [[YBTextFild alloc]initWithFrame:CGRectMake(20, 150, KScreen_W-180, 50)];
    self.loginTF_pass.maxLength = 4;
    self.loginTF_pass.font = Font(15);
    self.loginTF_pass.textColor = [UIColor whiteColor];
    self.loginTF_pass.keyboardType = UIKeyboardTypeNumberPad;
    self.loginTF_pass.layer.cornerRadius = 8;
    self.loginTF_pass.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 8, 50)];
    self.loginTF_pass.leftViewMode = UITextFieldViewModeAlways;
    self.loginTF_pass.tintColor = MainColor;
    self.loginTF_pass.backgroundColor = subviewback_black;
       UILabel * oldshowSSSSTip2 = [[UILabel alloc]init];
    oldshowSSSSTip2.clipsToBounds = YES;
    oldshowSSSSTip2.text = @"kass显示区";
    oldshowSSSSTip2.textColor = ColorHex(@"#f0f0f0");
    oldshowSSSSTip2.textAlignment = NSTextAlignmentCenter;
    oldshowSSSSTip2.font = [UIFont systemFontOfSize:28];
    oldshowSSSSTip2.hidden = YES;
    [self.view addSubview:oldshowSSSSTip2];
    [self.view addSubview:self.loginTF_pass];
        
    self.loginBtn_pass = [[UIButton alloc]initWithFrame:CGRectMake(KScreen_W-145, 150, 125, 50)];
    self.loginBtn_pass.titleLabel.font = Font(15);
    [self.loginBtn_pass setTitleColor:TextColor_wite forState:UIControlStateNormal];
    [self.loginBtn_pass addTarget:self action:@selector(loginBtn_passClick) forControlEvents:UIControlEventTouchUpInside];
    self.loginBtn_pass.layer.cornerRadius = 8;
    self.loginBtn_pass.backgroundColor = MainColor;
       UILabel * oldshowSSSSTip5 = [[UILabel alloc]init];
    oldshowSSSSTip5.clipsToBounds = YES;
    oldshowSSSSTip5.text = @"kass显示区";
    oldshowSSSSTip5.textColor = ColorHex(@"#f0f0f0");
    oldshowSSSSTip5.textAlignment = NSTextAlignmentCenter;
    oldshowSSSSTip5.font = [UIFont systemFontOfSize:28];
    oldshowSSSSTip5.hidden = YES;
    [self.view addSubview:oldshowSSSSTip5];
    [self.view addSubview:self.loginBtn_pass];
    
    self.loginBtn_login = [[UIButton alloc]initWithFrame:CGRectMake(20, 230, KScreen_W-40, 50)];
    self.loginBtn_login.titleLabel.font = Font(15);
    [self.loginBtn_login setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.loginBtn_login addTarget:self action:@selector(loginBtn_loginClick) forControlEvents:UIControlEventTouchUpInside];
    self.loginBtn_login.layer.cornerRadius = 8;
    self.loginBtn_login.backgroundColor = MainColor;
       UILabel * oldshowSSSSTip3 = [[UILabel alloc]init];
    oldshowSSSSTip3.clipsToBounds = YES;
    oldshowSSSSTip3.text = @"kass显示区";
    oldshowSSSSTip3.textColor = ColorHex(@"#f0f0f0");
    oldshowSSSSTip3.textAlignment = NSTextAlignmentCenter;
    oldshowSSSSTip3.font = [UIFont systemFontOfSize:28];
    oldshowSSSSTip3.hidden = YES;
    [self.view addSubview:oldshowSSSSTip3];
    [self.view addSubview:self.loginBtn_login];
    
//        UIButton * tipLabel1 = [[UIButton alloc]initWithFrame:CGRectMake(15, KScreen_H-KTab_H-70, KScreen_W-30, 80)];
//        [tipLabel1 setTitle:NSLocalizedString(@"登录即代表同意服务和隐私条款", nil) forState:UIControlStateNormal];
//        [tipLabel1 setTitleColor:COLOR_ALPH(255, 255, 255, 0.6) forState:UIControlStateNormal];
//        [tipLabel1 setTitleColor:COLOR_ALPH(255, 255, 255, 0.4) forState:UIControlStateHighlighted];
//        tipLabel1.titleLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightBold];
//        [tipLabel1 addTarget:self action:@selector(Agreement) forControlEvents:UIControlEventTouchUpInside];
//        tipLabel1.hidden = YES;
//    [self.view addSubview:tipLabel1];
    
    tipLabel.text = [NSString stringWithFormat:@"当前手机号码：+86 %@",[QZUserInfoModel share].username];
    self.loginTF_name.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入新手机号" attributes:@{NSForegroundColorAttributeName:ColorHex_Alpha(@"FFFFFF", 0.72),NSFontAttributeName:Font(15)}];
    self.loginTF_pass.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入验证码" attributes:@{NSForegroundColorAttributeName:ColorHex_Alpha(@"FFFFFF", 0.72),NSFontAttributeName:Font(15)}];
    [self.loginBtn_pass setTitle:@"获取验证码" forState:UIControlStateNormal];
    [self.loginBtn_login setTitle:@"更换" forState:UIControlStateNormal];
}
- (void)loginBtn_passClick{
    if (self.loginTF_name.text.length == 0) {
        [YBMsgPop keyboardshowPop:@"请输入新手机号"];
        return;
    }
    if (self.loginTF_name.text.length !=13) {
        [YBMsgPop keyboardshowPop:@"新手机号输入错误"];
        return;
    }
    self.loginBtn_pass.userInteractionEnabled = NO;
    [self.view endEditing:NO];
    [SVProgressHUDTool showWithStatus:nil];
    KMutableDic(sendDic)
    sendDic[@"phone"] = [self.loginTF_name.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    sendDic[@"event"] = @"edit_phone";
    sendDic[@"+86"] = @"country_code";
    _passNum = 1;
    KWeakSelf
    [YBNetBase sendSMSRequest:sendDic successBlock:^(NSDictionary * _Nonnull data, NSInteger code, NSString * _Nonnull msg) {
        [SVProgressHUDTool dismiss];
        if(code == 200){
            [YBMsgPop showPop:NSLocalizedString(@"发送成功", nil)];
            [weakSelf.cutTimer invalidate];
            weakSelf.cutTimerNum = 90;
            [weakSelf.loginTF_pass becomeFirstResponder];
            weakSelf.cutTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(doCutTimer) userInfo:nil repeats:YES];
        }else{
            [SVProgressHUDTool dismiss];
            [YBMsgPop showPop:msg];
        }
    }];
}
- (void)doCutTimer{
    _cutTimerNum -- ;
    if (_cutTimerNum <= 0) {
        [_loginBtn_pass setTitle:NSLocalizedString(@"获取验证码", nil) forState:UIControlStateNormal];
        _loginBtn_pass.userInteractionEnabled = YES;
        [_cutTimer invalidate];
    }else{
        [_loginBtn_pass setTitle:[NSString stringWithFormat:@"%dS",_cutTimerNum] forState:UIControlStateNormal];
    }
}
- (void)loginBtn_loginClick{
    if (self.loginTF_name.text.length == 0) {
        [YBMsgPop keyboardshowPop:NSLocalizedString(@"请输入手机号", nil)];
        return;
    }
    if (self.loginTF_name.text.length !=13) {
        [YBMsgPop keyboardshowPop:NSLocalizedString(@"手机号输入错误", nil)];
        return;
    }
    if (self.loginTF_pass.text.length == 0) {
        [YBMsgPop keyboardshowPop:NSLocalizedString(@"请输入验证码", nil)];
        return;
    }
    [self.view endEditing:NO];
    [SVProgressHUDTool showWithStatus:nil];
    KWeakSelf
    KMutableDic(sendDic)
    sendDic[@"phone"] = [self.loginTF_name.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    sendDic[@"code"] = self.loginTF_pass.text;
    sendDic[@"source"] = @"iOS";
    sendDic[@"device_no"] = [NSString stringWithFormat:@"IOS%.2f",[[[UIDevice currentDevice] systemVersion]  floatValue]];
    [YBNetBase editPhoneRequest:sendDic successBlock:^(NSDictionary * _Nonnull data, NSInteger code, NSString * _Nonnull msg) {
        if(code == 200){
            /** 缓存数据 */
            [QZUserInfoModel cleanUserData];
            [YBMsgPop showPop:@"手机号码修改成功，请重新登录"];
        }else{
            if(weakSelf.passNum == 0){
                [YBMsgPop showPop:@"请点击获取验证码"];
            }else{
                [YBMsgPop showPop:msg];
            }
           
        }
    }];
    
}

@end
